package com.mjt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjt.admin.dto.TreeDto;
import com.mjt.admin.mapper.GoodsMapper;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.GoodsType;
import com.mjt.admin.mapper.GoodsTypeMapper;
import com.mjt.admin.service.IGoodsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjt.admin.utils.AssertUtil;
import com.mjt.admin.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品类别表 服务实现类
 * </p>
 *
 * @author 莫某人
 * @since 2021-11-01
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public List<TreeDto> queryAllGoodsTypes() {

        List<TreeDto> treeDtos= goodsTypeMapper.queryAllGoodsTypes();


        return treeDtos;
    }

    /**
     * @description 根据pid查询分类信息
     * @author 莫某人
     * @date 11:04 2021/11/2
 * @param pId
     * @return {@link List< GoodsType>}
     **/
    @Override
    public List<GoodsType> getByPid(Integer pId) {
        return this.baseMapper.selectList(new QueryWrapper<GoodsType>().eq("p_id",pId));

    }

    @Override
    public RespDataBean<GoodsType> goodsTypeList() {
        List<GoodsType> list = this.list();

        return new RespDataBean<GoodsType>(0,"查询成功",list);
    }

    @Override
    public void saveGoodsTypePage(GoodsType goodsType) {

        AssertUtil.isTrue(StringUtil.isEmpty(goodsType.getName()),"类别名不能为空");

        goodsType.setState(0);
        goodsType.setIcon("icon-folder");
        AssertUtil.isTrue(!this.save(goodsType),"添加失败");

    }

    @Override
    public void deleteGoodsType(Integer id) {
        List<GoodsType> byPid = this.getByPid(id);
        AssertUtil.isTrue(byPid.size()!=0,"当前类别含有子类别，无法删除！");

        AssertUtil.isTrue(!this.removeById(id),"删除失败");


    }


}
